<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
$MAXIMUM_FILESIZE = 300 * 1024 * 1024; // 300,000,000 Byte
$MAXIMUM_POSTSIZE = 300 * 1024 * 1024; // 300,000,000 Byte

//
if( isset($_FILES['Filedata']) && $_FILES['Filedata']['size'] <= $MAXIMUM_FILESIZE ) {
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.upload.class.php');
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.file.class.php');
	include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'thumb.function.php');
	
	//
	$uploadObj = new CoreUpload('Filedata');
	$uploadObj->setDestination(CONFIG_PATH . CONFIG_DIR_UPLOADS_ORIGINS);
	$uploadObj->setRandFilename(false);
	if( $uploadObj->parse() && $uploadObj->save() ) {
		$upload_filename = $uploadObj->save_filename;
		$upload_save_file = $uploadObj->save_file;
		
		$up_filename = $uploadObj->filename;
		$up_filetype = $uploadObj->filetype;
		$up_filesize = $uploadObj->filesize;
		
		//
		$upload_fileObj = new CoreFile($upload_save_file);
		$upload_fileObj->parse();
		
		$image = '';
		$image_width = $image_height = 0;
		if( $upload_fileObj->is_image == true ) {
			$image = '1';
			$image_attribute = $upload_fileObj->getFileAttribute();
			$image_width = $upload_fileObj->getImageWidth();
			$image_height = $upload_fileObj->getImageHeight();
		}
		
		//
		$data_array = array(
					'resources_filename' => $up_filename,
					'resources_filetype' => $up_filetype,
					'resources_filesize' => $up_filesize,
					'resources_value' => $upload_filename,
					//
					'resources_image' => $image,
					'resources_image_width' => $image_width,
					'resources_image_height' => $image_height,
					'resources_width' => $image_width,
					'resources_height' => $image_height,
					//
					'resources_name' => $up_filename,
					'resources_description' => '',
					'resources_status' => '1',
						);
		$resources_id = $resObj->addItem($data_array);
		
		//
		if( $upload_fileObj->is_image == true ) {
			//thumbs
			$thumb_file = CONFIG_PATH . CONFIG_DIR_UPLOADS_THUMBS . $upload_filename;
			$thumb_width = CONST_IMAGE_THUMB_WIDTH;
			$thumb_height = CONST_IMAGE_THUMB_HEIGHT;
			func_thumb_generate_thumb($upload_save_file, $thumb_file, $thumb_width, $thumb_height);
			
			//resized
			$thumb_file = CONFIG_PATH . CONFIG_DIR_UPLOADS_RESIZED . $upload_filename;
			$thumb_width = CONST_IMAGE_RESIZE_WIDTH;
			$thumb_height = CONST_IMAGE_RESIZE_HEIGHT;
			func_thumb_generate_thumb($upload_save_file, $thumb_file, $thumb_width, $thumb_height);
			
			//
			$result = CONFIG_DIR_UPLOADS_THUMBS . ':' . $upload_filename . ':' . $image_width . ':' . $image_height;
			$result .= ':' . $resources_id;
		} else {
			$result = $upload_filename;
		}
	} else {
		$result = 'fail';
	}
} else {
	$result = 'fail';
}
echo $result;
exit();
//
?>